//
//  testViewController.m
//  VICKY ZONE
//
//  Created by sunx on 15/4/6.
//  Copyright (c) 2015年 sunx. All rights reserved.
//

#import "testViewController.h"
#import "OSSClient.h"
#import "OSSTool.h"

@interface testViewController ()

@end

@implementation testViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    OSSClient *ossclient = [OSSClient sharedInstanceManage];
    NSString *accessKey = @"FNjUQUOIou5tLmjT";
    NSString *secretKey = @"nonrU2qLkukyPcqyEbXwAlUrcylH8G";
    NSString *yourBucket = @"vickyzone";
    NSString *yourKey = @"hello.png";
    
    [ossclient setGenerateToken:^(NSString *method, NSString *md5, NSString *type, NSString *date, NSString *xoss, NSString *resource){
        NSString *signature = nil;
        NSString *content = [NSString stringWithFormat:@"%@\n%@\n%@\n%@\n%@%@", method, md5, type, date, xoss, resource];
        signature = [OSSTool calBase64Sha1WithData:content withKey:secretKey];
        signature = [NSString stringWithFormat:@"OSS %@:%@", accessKey, signature];
        NSLog(@"here signature:%@", signature);
        return signature;
    }];
    [ossclient setGlobalDefaultBucketHostId:@"oss-cn-beijing.aliyuncs.com"];
    [ossclient setGlobalDefaultBucketAcl:PUBLIC_READ_WRITE];
    
    
    OSSBucket *bucket = [[OSSBucket alloc] initWithBucket:yourBucket];
    [bucket setAcl:PUBLIC_READ_WRITE];
    [bucket setOssHostId:@"oss-cn-beijing.aliyuncs.com"];
    
    NSError *error = nil;
    
    UIImage *image = [UIImage imageNamed:@"icon"];
    NSData *data = UIImageJPEGRepresentation(image, 0.7);
    
    OSSData *testData = [[OSSData alloc] initWithBucket:bucket withKey:yourKey];
    
    [testData setData:data withType:@"image/png"];
    [testData uploadWithUploadCallback:^(BOOL isSuccess, NSError *error) {
        if (isSuccess) {
            NSLog(@"complete...");
            NSString *url = [testData getResourceURL];
            NSLog(@"%@",url);
        }
        else
        {
            NSLog(@"errorInfo_testDataUploadWithProgress:%@", [error userInfo]);
        }
    } withProgressCallback:^(float progress) {
        NSLog(@"current get %f", progress);
    }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
